package lovo.practice_7_14.frame;

import lovo.Util.Util;
import lovo.practice_7_14.pojo.UserDO;
import lovo.practice_7_14.service.UserService;
import lovo.practice_7_14.service.impl.UserService.UserServiceImpl;
import lovo.practice_7_14.util.DBUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ScanPanel extends JPanel implements ActionListener {


    private int falsetime = 3;

    public int getFalsetime() {
        return falsetime;
    }

    public void setFalsetime(int falsetime) {
        this.falsetime = falsetime;
    }

    private J189_7_14 frame;
    private JTextField jTextFieldAccount;
    private JPasswordField jPasswordField;
    private JButton jButtonLogin;
    private Icon ImageLogin = Util.getimagesIcon("/Images/buttonLand.jpg");


    public ScanPanel(J189_7_14 frame) {

        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jTextFieldAccount = new JTextField();
        jTextFieldAccount.setBounds(400, 400, 150, 40);
        this.add(jTextFieldAccount);
        jPasswordField = new JPasswordField();
        jPasswordField.setBounds(400, 500, 150, 40);
        this.add(jPasswordField);

        jButtonLogin = new JButton(ImageLogin);
        jButtonLogin.setBounds(600, 500, 140, 50);
        this.add(jButtonLogin);
        jButtonLogin.addActionListener(this);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtonLogin) {
//            for (int i = 0; i < frame.getListusers().size(); i++) {
//                if (jTextFieldAccount.getText().equals(frame.getListusers().get(i).getAccount()) && jPasswordField.getText().equals(frame.getListusers().get(i).getPwd())) {
//                     frame.setUsers(frame.getListusers().get(i));
//                    frame.switchPanel("mune");
//                    return;
//                }
//            }
            String username = jTextFieldAccount.getText();
            String pwd = jPasswordField.getText();
            DBUtil dbUtil = new DBUtil();
//            UserDO userDO = dbUtil.getByUsernameAndPwd(username, pwd);
            UserService userService =new UserServiceImpl();
            UserDO userDO = userService.getByUsernameAndPwd(username, pwd);
            if (userDO != null) {
                frame.setUsers(userDO);
                frame.switchPanel("mune");
                return;
            }
            falsetime--;
            if (falsetime == 0) {
                JOptionPane.showMessageDialog(this, "3次机会用完，你卡没了");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "登录失败，你还有" + falsetime + "次机会");


        }


    }
}




